<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="管理员uid" prop="adminUid">
      <el-input v-model="dataForm.adminUid" placeholder="管理员uid"></el-input>
    </el-form-item>
    <el-form-item label="请求ip地址" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="请求ip地址"></el-input>
    </el-form-item>
    <el-form-item label="请求url" prop="url">
      <el-input v-model="dataForm.url" placeholder="请求url"></el-input>
    </el-form-item>
    <el-form-item label="请求方式" prop="type">
      <el-input v-model="dataForm.type" placeholder="请求方式"></el-input>
    </el-form-item>
    <el-form-item label="请求类路径" prop="classPath">
      <el-input v-model="dataForm.classPath" placeholder="请求类路径"></el-input>
    </el-form-item>
    <el-form-item label="请求方法名" prop="method">
      <el-input v-model="dataForm.method" placeholder="请求方法名"></el-input>
    </el-form-item>
    <el-form-item label="请求参数" prop="params">
      <el-input v-model="dataForm.params" placeholder="请求参数"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="operation">
      <el-input v-model="dataForm.operation" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="ip来源" prop="ipSource">
      <el-input v-model="dataForm.ipSource" placeholder="ip来源"></el-input>
    </el-form-item>
    <el-form-item label="方法请求花费的时间" prop="spendTime">
      <el-input v-model="dataForm.spendTime" placeholder="方法请求花费的时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          userName: '',
          adminUid: '',
          ip: '',
          url: '',
          type: '',
          classPath: '',
          method: '',
          params: '',
          operation: '',
          status: '',
          createTime: '',
          updateTime: '',
          ipSource: '',
          spendTime: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          adminUid: [
            { required: true, message: '管理员uid不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: '请求ip地址不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '请求url不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请求方式不能为空', trigger: 'blur' }
          ],
          classPath: [
            { required: true, message: '请求类路径不能为空', trigger: 'blur' }
          ],
          method: [
            { required: true, message: '请求方法名不能为空', trigger: 'blur' }
          ],
          params: [
            { required: true, message: '请求参数不能为空', trigger: 'blur' }
          ],
          operation: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          ipSource: [
            { required: true, message: 'ip来源不能为空', trigger: 'blur' }
          ],
          spendTime: [
            { required: true, message: '方法请求花费的时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/syslog/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.sysLog.userName
                this.dataForm.adminUid = data.sysLog.adminUid
                this.dataForm.ip = data.sysLog.ip
                this.dataForm.url = data.sysLog.url
                this.dataForm.type = data.sysLog.type
                this.dataForm.classPath = data.sysLog.classPath
                this.dataForm.method = data.sysLog.method
                this.dataForm.params = data.sysLog.params
                this.dataForm.operation = data.sysLog.operation
                this.dataForm.status = data.sysLog.status
                this.dataForm.createTime = data.sysLog.createTime
                this.dataForm.updateTime = data.sysLog.updateTime
                this.dataForm.ipSource = data.sysLog.ipSource
                this.dataForm.spendTime = data.sysLog.spendTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/syslog/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'userName': this.dataForm.userName,
                'adminUid': this.dataForm.adminUid,
                'ip': this.dataForm.ip,
                'url': this.dataForm.url,
                'type': this.dataForm.type,
                'classPath': this.dataForm.classPath,
                'method': this.dataForm.method,
                'params': this.dataForm.params,
                'operation': this.dataForm.operation,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'ipSource': this.dataForm.ipSource,
                'spendTime': this.dataForm.spendTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
